package com.teatrace.action.produce;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.teatrace.model.base.Device;
import com.teatrace.model.base.Employee;
import com.teatrace.model.base.Field;
import com.teatrace.model.base.TeaTreeType;
import com.teatrace.model.produce.TeaTreePlant;
import com.teatrace.model.produce.TeaTrees;
import com.teatrace.service.basic.BasicService;
import com.teatrace.service.basic.impl.BasicServiceImpl;
import com.teatrace.service.produce.ProduceService;
import com.teatrace.service.produce.impl.ProduceServiceImpl;

public class PlantAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger log = Logger.getLogger(PlantAction.class);
	
	private int teaTreesId;
	private int count;
	private int teaTreeTypeId;

	private String plantEmps;
	private String plantDevices;

	private String plantTime;
	private String description;
	
	private int fieldId;
	

	public ProduceService produceService;

	public PlantAction(){

		produceService = new ProduceServiceImpl();
	}

	/**
	 * 空地块列表
	 * @return
	 */
	public String listFreeField(){
		log.debug("列出空地块");
		try {
			List<Field> freeFieldList = produceService.findBlankFieldsByCriteria(Field.class, 1);
			HttpServletRequest requet = ServletActionContext.getRequest();
			requet.setAttribute("freeFieldList", freeFieldList);	
		} catch (Exception e) {
			log.debug(e.getMessage());
			e.printStackTrace();
		}	
		return SUCCESS;
	}
	
	/**
	 * 添加种植信息
	 * @return
	 */
	public String addPlantInfo() {
		log.debug("添加种植信息");	
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("msg", "添加种植信息");
		try {
			log.debug("------------------"+this.getTeaTreePlant().getDescription());
			
			
			produceService.add(this.getTeaTrees());
			
			BasicService baseic = new BasicServiceImpl();
			Field field = (Field) baseic.getById(Field.class, fieldId);
			field.setFree(true);
			baseic.update(field);
			
			
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	
	
	
	
	public TeaTreePlant getTeaTreePlant() throws Exception{
		
		TeaTreePlant plant = new TeaTreePlant();
		plant.setDescription(this.getDescription());
		plant.setPlantTime(plantTime);
		
		plant.setEmps(this.getEmployee());
		plant.setDevices(this.getDevice());
		
		return plant;
	}
	
	public TeaTrees getTeaTrees() throws Exception{
		//load茶树的种类
		TeaTreeType teaTreeType = (TeaTreeType) produceService.loadById(TeaTreeType.class, teaTreeTypeId);
		log.debug("地块的编号---------------"+fieldId);
		log.debug("茶树数量---------------"+count);
		Field field = (Field) produceService.loadById(Field.class, fieldId);
//		field.setFree(true);
//		produceService.update(field);	
		TeaTrees teaTrees = new TeaTrees();
		teaTrees.setId(teaTreesId);
		teaTrees.setCount(count);
		teaTrees.setField(field);
		teaTrees.setPickable(false);
		teaTrees.setStatus("刚种植");
		teaTrees.setPlantTime(plantTime);
		teaTrees.setTeaTreeType(teaTreeType);
		
		this.getTeaTreePlant().addTeaTrees(teaTrees);
		teaTrees.setTeaTreePlant(this.getTeaTreePlant());

		return teaTrees;
	}
	
	public Set<Employee> getEmployee() throws Exception{
		Set<Employee> employeeSet = new HashSet<Employee>();
		if(plantEmps.contains(",")){
			String[] employees = plantEmps.split(",");
			for(int i = 0; i<employees.length;i++){
				Employee employee =(Employee) produceService.loadById(Employee.class, Integer.parseInt(employees[i]));
				employeeSet.add(employee);
			}
		}else{
			Employee employee =(Employee) produceService.loadById(Employee.class,Integer.parseInt(plantEmps));
			employeeSet.add(employee);
		}
		return employeeSet;
	}
	
	
	public Set<Device> getDevice() throws Exception{
		Set<Device> deviceSet = new HashSet<Device>();
		if(plantDevices.contains(",")){
			String[] devicesArray = plantDevices.split(",");
			for(int i = 0; i<devicesArray.length;i++){
				Device device =(Device) produceService.loadById(Device.class,Integer.parseInt(devicesArray[i]));
				deviceSet.add(device);
			}
		}else{
			Device device =(Device) produceService.loadById(Device.class, Integer.parseInt(plantDevices));
			deviceSet.add(device);
		}
		return deviceSet;
	}
	
	
	
	//------------setter and getter-----------------
	
	public void setTeaTreesId(int teaTreesId) {
		this.teaTreesId = teaTreesId;
	}

	public int getTeaTreesId() {
		return teaTreesId;
	}

	public int getTeaTreeTypeId() {
		return teaTreeTypeId;
	}

	public void setTeaTreeTypeId(int teaTreeTypeId) {
		this.teaTreeTypeId = teaTreeTypeId;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}



	public String getPlantTime() {
		return plantTime;
	}

	public void setPlantTime(String plantTime) {
		this.plantTime = plantTime;
	}



	public String getPlantEmps() {
		return plantEmps;
	}



	public void setPlantEmps(String plantEmps) {
		this.plantEmps = plantEmps;
	}



	public String getPlantDevices() {
		return plantDevices;
	}



	public void setPlantDevices(String plantDevices) {
		this.plantDevices = plantDevices;
	}



	public int getFieldId() {
		return fieldId;
	}



	public void setFieldId(int fieldId) {
		this.fieldId = fieldId;
	}


}
